package fr.gest4team.app.pilotage.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.gest4team.app.pilotage.metier.VersionOm;
import fr.gest4team.app.pilotage.service.ServiceTache;
import fr.gest4team.app.pilotage.service.ServiceVersion;

/**
 * Servlet implementation class VersionServlet
 */
public class VersionServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */
    public VersionServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
        IOException {
        execute(request, response);
    }

    private void execute(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
        IOException {
        String dispatcher = "";
        final String action = request.getParameter("action");
        try {
            if (action == null || "lister-all".equalsIgnoreCase(action)) {
                final List<VersionOm> liste = ServiceVersion.getInstance().chargerVersions();
                request.setAttribute("VERSIONS", liste);
                dispatcher = "/versions.jsp";
            } else if ("create".equalsIgnoreCase(action)) {
                final String projet = request.getParameter("projet");
                final String version = request.getParameter("nom");
                final String dateRe7 = request.getParameter("dateRe7");
                final String dateClient = request.getParameter("dateClient");
                final SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
                final VersionOm om = new VersionOm();
                om.setProjet(Integer.parseInt(projet));
                om.setNom(version);
                om.setDateLivraisonRe7((dateRe7.equals("")) ? null : format.parse(dateRe7));
                om.setDateLivriaosnClient((dateClient.equals("")) ? null : format.parse(dateClient));
                ServiceVersion.getInstance().ajouterVersion(om);

                final List<VersionOm> liste = ServiceVersion.getInstance().chargerVersions();
                request.setAttribute("VERSIONS", liste);
                dispatcher = "/versions.jsp";
            } else if ("delete".equalsIgnoreCase(action)) {

                final String version = request.getParameter("version");
                final String id = request.getParameter("idversion");
                ServiceTache.getInstance().supprimerVersion(id);
                ServiceVersion.getInstance().supprimerVersion(version);

                final List<VersionOm> liste = ServiceVersion.getInstance().chargerVersions();
                request.setAttribute("VERSIONS", liste);
                dispatcher = "/versions.jsp";
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
        final RequestDispatcher rd = request.getRequestDispatcher(dispatcher);
        rd.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {
        execute(request, response);
    }

}
